<?php
/**
* Admin_ResourcesController
*
* @author Petr Laslo
* @license MIT License
* @package Knihovna\Admin\Controllers
* @link https://code.google.com/p/knihovna/
*/
class Admin_ResourcesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $form = new Admin_Form_DeleteResource();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try{
                $post = $this->getRequest()->getPost('id');
                $resource = new Knihovna_Model_DbTable_Usersresources();
                $where = $resource->getAdapter()->quoteInto('nazev = ?', $post);
                $resource->delete($where);
                $this->_helper->flashMessenger('success|Zdroj byl úspěšně smazán.');
                $this->_redirect('/admin/resources/');
            }catch(Zend_Db_Exception $error){
                $this->_helper->flashMessenger('error|'.$error->getMessage());
                $this->_redirect('/admin/resources/');
            }
        }elseif($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))){
            $this->_redirect('/admin/resources/');
        }

        $this->view->form = $form;
        $roles = new Knihovna_Model_DbTable_Usersresources();
        $select = $roles->select()
            ->order('nazev');
        $resourceslist = $roles->fetchAll($select);
        $this->view->resources = $resourceslist;

        //TODO paginator

        //vypsání flashMessage
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
    }

    /**
     * Přidání zdroje
     *
     * @author Petr Laslo
     * @return void
     */
    public function addAction()
    {
        $form = new Admin_Form_AddResource();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $resource = new Knihovna_Model_DbTable_Usersresources();
                    $data = array(
                        'nazev' => $post['resource'],
                        'rodic' => $post['parent'],
                    );
                    $resource->insert($data);

                    $this->_helper->flashMessenger('success|Zdroj "'.$post['resource'].'" byla úspěšně vytvořen.');
                    $this->_redirect('/admin/resources/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                    $this->_redirect('/admin/resources/');
                }
            }
        }

        $this->view->form = $form;
    }

    /**
     * Editace zdroje
     *
     * @author Petr Laslo
     * @return void
     */
    public function editAction()
    {
        $form = new Admin_Form_AddResource();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $resource = new Knihovna_Model_DbTable_Usersresources();
                    $data = array(
                        'nazev' => $post['resource'],
                        'rodic' => $post['parent'],
                    );

                    $where = $resource->getAdapter()->quoteInto('nazev = ?', $this->_getParam('id'));
                    $resource->update($data,$where);
                    $this->_helper->flashMessenger('success|Zdroj byl úspěšně editován.');
                    $this->_redirect('/admin/resources/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                    $this->_redirect('/admin/resources/');
                }
            }
        }else{
            $id = $this->_getParam('id', 0);
            if (!empty($id)) {
                $resource = new Knihovna_Model_DbTable_Usersresources();
                $where = $resource->getAdapter()->quoteInto('nazev = ?', $id);
                $form->populate($resource->getResource($where));
            }
        }
        $this->view->form = $form;
    }
}

